package cn.damai.search.service.impl;

import cn.damai.common.*;
import cn.damai.model.*;
import cn.damai.search.baseService.*;
import cn.damai.search.vo.DmAreaVo;
import cn.damai.search.vo.DmItemTypeVo;
import cn.damai.search.vo.ItemSearchVo;
import cn.damai.search.service.ItemSearchService;
import cn.damai.search.vo.SearchParametersVo;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class ItemSearchServiceIpml implements ItemSearchService {

    @Autowired
    private BaseItemService itemService;
    @Autowired
    private BaseImageService imageService;
    @Autowired
    private BaseCinemaService cinemaService;
    @Autowired
    private BaseAreaService areaService;
    @Autowired
    private BaseItemSearchService itemSearchService;
    @Autowired
    private BaseItemTypeService itemTypeService;

    /**
     *  添加查询数据
     */
    @Override
    public void importItemList() {
        Map<String, Object> params = new HashMap<>();
       params.put("beginPos",0);
//        params.put("pageSize",500);

        List<Item> itemList = itemService.getItemListByMap(params);

        for (Item item:itemList){
            ItemSearchVo itemSearchVo = new ItemSearchVo();
            BeanUtils.copyProperties(item,itemSearchVo);
            itemSearchVo.setId(item.getId());
            //查询图片信息
            List<Image> ImageList = imageService.queryImageList(item.getId(),
                    Constants.Image.ImageType.carousel,
                    Constants.Image.ImageCategory.item);
            //组装图片信息
            itemSearchVo.setImgUrl(EmptyUtils.isEmpty(ImageList)? null:ImageList.get(0).getImgUrl());

            //组装开始时间和结束时间

            itemSearchVo.setStartTime(item.getStartTime().getTime()+"");
            itemSearchVo.setEndTime(item.getEndTime().getTime()+"");
            //组装最高价格和最低价格
            itemSearchVo.setMaxPrice(item.getMaxPrice());
            itemSearchVo.setMinPrice(item.getMinPrice());
            //商品类型分类和子类
            itemSearchVo.setItemTypeId1(item.getItemType1Id());
            itemSearchVo.setItemTypeId2(item.getItemType2Id());
            //查询剧场信息
            Cinema cinema = cinemaService.getCinemaById(item.getCinemaId());
            //组装区域id
            itemSearchVo.setAddress(cinema.getAddress());
            itemSearchVo.setAreaId(cinema.getAreaId());
            itemSearchVo.setAreaName(cinema.getAreaName());
            //添加数据
            itemSearchService.insertItemsearch(itemSearchVo);
        }
    }

    /**
     *  查询数据
     */
    @Override
    public  Page<ItemSearchVo> queryItemList(Integer curPage,Map<String,Object> params) {
       // System.out.println("搜索条件个数"+params.size());
        Integer itemNum = itemSearchService.getItemsearchCountByMap(params);
        //page(当前页面,页面行数,商品总数)
        Page page = new Page(curPage,20,itemNum);

        //查询城市
            params.put("beginPos",page.getBeginPos());
            params.put("pageSize",page.getPageSize());
            List<ItemSearchVo>  itemsearchList = itemSearchService.getItemsearchListByMap(params);

        if(params.containsKey("text")){
            //System.out.println("模糊查询");
        }

        page.setRows(itemsearchList);

        return page;
    }

    /**
     *   查询城市
     */
    @Override
    public List<DmAreaVo> queryArea() {
        Map<String, Object> params = new HashMap<>();
       //此方法是组装 区域信息
        List<Area> areaList = areaService.getAreaListByMap(params);

        List<DmAreaVo> areaVoList = new ArrayList<>();
        //遍历数据
        for (Area area :areaList){
            DmAreaVo dmAreaVo = new DmAreaVo();
            dmAreaVo.setId(area.getId());
            dmAreaVo.setName(area.getName());
            areaVoList.add(dmAreaVo);
        }
        return areaVoList;
    }

    /**
     *   查询商品分类
     */
    @Override
    public List<DmItemTypeVo> queryItemType(String param) {
        Map<String, Object> params = new HashMap<>();
        params.put("parent",param);
        //此方法是组装 分类信息
        List<ItemType> itemTypeList = itemTypeService.getItemTypeListByMap(params);
       // System.out.println(itemTypeList.size());
        List<DmItemTypeVo> itemTypeVoList = new ArrayList<>();
        //遍历数据
        for (ItemType itemType :itemTypeList){
            DmItemTypeVo dmItemTypeVo = new DmItemTypeVo();
                dmItemTypeVo.setId(itemType.getId());
                dmItemTypeVo.setItemType(itemType.getItemType());
                dmItemTypeVo.setLevel(itemType.getLevel());
                dmItemTypeVo.setParent(itemType.getParent());
                dmItemTypeVo.setAliasName(itemType.getAliasName());
                itemTypeVoList.add(dmItemTypeVo);
        }
        return itemTypeVoList;
    }

    /**
     *  查询子类个数
     */
    @Override
    public Integer queryItemCount(String itemType1) {
        Map<String, Object> params = new HashMap<>();
        params.put("parent",itemType1);
        Integer itemType2 = itemTypeService.getItemTypeCountByMap(params);
        return itemType2;
    }

//    模糊查询 - 城市 分类
    @Override
    public SearchParametersVo queryGrouping(Map<String, Object> params) {
        SearchParametersVo searchParametersVo =new SearchParametersVo();


        //组装城市
        List<ItemSearchVo> itemSearchVoList = itemSearchService.queryAreaGrouping(params);
        List<DmAreaVo> areaVoList = new ArrayList<>();
        for (int i=0;i<itemSearchVoList.size();i++) {
            DmAreaVo dmAreaVo = new DmAreaVo();
            Area area = areaService.getAreaById(itemSearchVoList.get(i).getAreaId());
            if(area != null) {
                dmAreaVo.setId(area.getId());
                dmAreaVo.setName(area.getName());
                areaVoList.add(dmAreaVo);
            }
        }
        searchParametersVo.setAreaVos(areaVoList);




        //组装商品分类
        List<ItemSearchVo> test = itemSearchService.queryItemTypeGrouping(params);
        List<DmItemTypeVo> itemTypeVoList = new ArrayList<>();
        for (int i=0;i<test.size();i++){
            DmItemTypeVo dmItemTypeVo = new DmItemTypeVo();
            ItemType itemType = itemTypeService.getItemTypeById(test.get(i).getItemTypeId1());
            if(itemType != null){
                dmItemTypeVo.setId(itemType.getId());
                dmItemTypeVo.setItemType(itemType.getItemType());
                dmItemTypeVo.setLevel(itemType.getLevel());
                dmItemTypeVo.setParent(itemType.getParent());
                dmItemTypeVo.setAliasName(itemType.getAliasName());
                itemTypeVoList.add(dmItemTypeVo);
            }
        }
        searchParametersVo.setItemType1Vos(itemTypeVoList);



        //组装子类
        List<ItemSearchVo> itemSearch1VoList = itemSearchService.queryItemType2Grouping(params);
        List<DmItemTypeVo> itemType1VoList = new ArrayList<>();
        for (int i=0;i<itemSearch1VoList.size();i++) {
            DmItemTypeVo dmItemTypeVo = new DmItemTypeVo();
            if(itemSearch1VoList.get(i).getItemTypeId2() != null){
                ItemType itemType = itemTypeService.getItemTypeById(itemSearch1VoList.get(i).getItemTypeId2());
                if (itemType != null) {
                    dmItemTypeVo.setId(itemType.getId());
                    dmItemTypeVo.setItemType(itemType.getItemType());
                    dmItemTypeVo.setLevel(itemType.getLevel());
                    dmItemTypeVo.setParent(itemType.getParent());
                    dmItemTypeVo.setAliasName(itemType.getAliasName());
                    itemType1VoList.add(dmItemTypeVo);
                }
            }
        }
        searchParametersVo.setItemType2Vos(itemType1VoList);


        return searchParametersVo;
    }

    @Override
    public List<DmItemTypeVo> queryItemTypeGrouping1(Map<String, Object> params) {
        //组装子类
        List<ItemSearchVo> itemSearch1VoList = itemSearchService.queryItemType2Grouping(params);
        List<DmItemTypeVo> itemType1VoList = new ArrayList<>();
        for (int i=0;i<itemSearch1VoList.size();i++) {
            DmItemTypeVo dmItemTypeVo = new DmItemTypeVo();
            if(itemSearch1VoList.get(i).getItemTypeId2() != null){
                ItemType itemType = itemTypeService.getItemTypeById(itemSearch1VoList.get(i).getItemTypeId2());
                if (itemType != null) {
                    dmItemTypeVo.setId(itemType.getId());
                    dmItemTypeVo.setItemType(itemType.getItemType());
                    dmItemTypeVo.setLevel(itemType.getLevel());
                    dmItemTypeVo.setParent(itemType.getParent());
                    dmItemTypeVo.setAliasName(itemType.getAliasName());
                    itemType1VoList.add(dmItemTypeVo);
                }
            }
        }

        return  itemType1VoList;
    }



}

